//
//  LoginViewController.m
//  YiChat20160706
//
//  Created by 李政 on 16/7/6.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "LoginViewController.h"

#import "RegisterViewController.h"

#import "HomeViewController.h"
#import "FileViewController.h"
#import "NewsViewController.h"
#import "PersonalViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.title = @"登录";
}
- (IBAction)loginButton:(id)sender
{
    if (![self checkIsNull])
    {
        return;
    }
    
    AFHTTPSessionManager *mgr = [NetTool getSessionManager];
    
    if (mgr)
    {
        
        NSDictionary *parameterDic = @{
                                       @"command":@"ST_L",
                                       @"name":_userNameTF.text,
                                       @"psw":_pswTF.text
                                       };
        
        
        [mgr POST:[self getNormalInterfaceUrl] parameters:parameterDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject)
            {
               NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
                if ([resultDic[@"result"] isEqualToString:@"1"])
                {
                    //跳转页面
                    
                    [self gotoHomeViewController];
                    //保存accessToken
                    
                    [self saveAccessToken:resultDic];
                    
                }
                else
                {
                    [Util showAlertWithMessage:resultDic[@"error"] confirmBlock:^
                    {
                        [_pswTF becomeFirstResponder];
                    }
                    ];
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [Util showAlertWithMessage:LOAD_FAIL confirmBlock:nil];
        }];
    }
    
}

- (void)gotoHomeViewController
{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    homeVC.tabBarItem.image = [UIImage imageNamed:@"main"];
    homeVC.tabBarItem.title = @"主页";
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:homeVC];
    
    
    NewsViewController *newsVC = [[NewsViewController alloc] init];
    newsVC.tabBarItem.image = [UIImage imageNamed:@"news"];
    newsVC.tabBarItem.title = @"新闻";
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:newsVC];
    
    FileViewController *fileVC = [[FileViewController alloc] init];
    fileVC.tabBarItem.image = [UIImage imageNamed:@"file"];
    fileVC.tabBarItem.title = @"文件";
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:fileVC];
    
    PersonalViewController *personalVC = [[PersonalViewController alloc] init];
    personalVC.tabBarItem.image = [UIImage imageNamed:@"person"];
    personalVC.tabBarItem.title = @"个人";
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:personalVC];
    
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    
    tabBar.viewControllers = @[nav1,nav2,nav3,nav4];
    
    WINDOW.rootViewController = tabBar;
    
    [UIView animateWithDuration:0.5 animations:^{
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:WINDOW cache:YES];
    }];
}

- (void)saveAccessToken:(NSDictionary *)dic
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *accessToken = dic[ACCESS_TOKEN];
    
    double time = [dic[@"time"] doubleValue];

    NSDate *loseDate = [NSDate dateWithTimeIntervalSinceNow:time];

    [userDefaults setObject:loseDate forKey:LOSE_TIME];
    
    [userDefaults setObject:accessToken forKey:ACCESS_TOKEN];
    
    [userDefaults synchronize];
}

- (BOOL)checkIsNull
{
    if ([_userNameTF.text isEqualToString:@""])
    {
        [Util showAlertWithMessage:USER_NAME_NOTNULL confirmBlock:^
        {
            [_userNameTF becomeFirstResponder];
        }];
        return NO;
    }
    
    if ([_pswTF.text isEqualToString:@""])
    {
        [Util showAlertWithMessage:PSW_NOTNULL confirmBlock:^
        {
            [_pswTF becomeFirstResponder];
        }];
        return NO;
    }
    
    return YES;
}

- (IBAction)registerButton:(id)sender
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    
    [self.navigationController pushViewController:registerVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
